package service.impl;

import dao.FruitDao;
import entity.Fruit;
import org.apache.ibatis.session.SqlSession;
import service.FruitService;
import utils.MybatisUtil;

import java.util.List;

public class FruitServiceImpl implements FruitService {
    @Override
    public List<Fruit> selectAllFruit(int pageIndex, int pageSize) {
        int startIndex = (pageIndex - 1) * pageSize;
        SqlSession sqlSession = null;
        List<Fruit> fruits = null;
        try{
            sqlSession = MybatisUtil.createSqlSession();
            fruits = sqlSession.getMapper(FruitDao.class).selectAllFruit(startIndex,pageSize);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MybatisUtil.closeSqlSession(sqlSession);
        }
        return fruits;
    }

    @Override
    public Fruit selectById(int fruitId) {
        SqlSession sqlSession = null;
        Fruit fruits = null;
        try{
            sqlSession = MybatisUtil.createSqlSession();
            fruits = sqlSession.getMapper(FruitDao.class).selectById(fruitId);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MybatisUtil.closeSqlSession(sqlSession);
        }
        return fruits;
    }

    @Override
    public int delete(int fruitId) {
        SqlSession sqlSession = null;
        int r = 0;
        try{
            sqlSession = MybatisUtil.createSqlSession();
            r = sqlSession.getMapper(FruitDao.class).delete(fruitId);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            MybatisUtil.closeSqlSession(sqlSession);
        }
        return r;
    }

    @Override
    public int count() {
        SqlSession sqlSession = null;
        int count = 0;
        try{
            sqlSession = MybatisUtil.createSqlSession();
            count = sqlSession.getMapper(FruitDao.class).count();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MybatisUtil.closeSqlSession(sqlSession);
        }
        return count;
    }

    @Override
    public int add(Fruit fruit) {
        SqlSession sqlSession = null;
        int r = 0;
        try{
            sqlSession = MybatisUtil.createSqlSession();
            r = sqlSession.getMapper(FruitDao.class).add(fruit);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            MybatisUtil.closeSqlSession(sqlSession);
        }
        return r;
    }

    @Override
    public int update(Fruit fruit) {
        SqlSession sqlSession = null;
        int r = 0;
        try{
            sqlSession = MybatisUtil.createSqlSession();
            r = sqlSession.getMapper(FruitDao.class).update(fruit);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            MybatisUtil.closeSqlSession(sqlSession);
        }
        return r;
    }

    @Override
    public List<Fruit> selectFruit(int pageIndex, int pageSize, String type) {
        int startIndex = (pageIndex - 1) * pageSize;
        SqlSession sqlSession = null;
        List<Fruit> fruits = null;
        try{
            sqlSession = MybatisUtil.createSqlSession();
            fruits = sqlSession.getMapper(FruitDao.class).selectFruit(startIndex,pageSize,type);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MybatisUtil.closeSqlSession(sqlSession);
        }
        return fruits;
    }
}
